package utils;

import java.util.ArrayList;
import java.util.List;

public class WishColumn<T>
{
	private String title = "";

	private int width = 10;

	private List<WishExcelFunction<?>> functions = new ArrayList<>();

	private List<WishColumn<?>> children = new ArrayList<>();

	public WishColumn<T> add(WishExcelFunction<?> item)
	{
		functions.add(item);
		return this;
	}

	public WishColumn<?> addChild(WishColumn<?> item)
	{
		children.add(item);
		return item;
	}

	public WishColumn<?> addChild(String title)
	{
		WishColumn<?> column = new WishColumn<>(title);
		children.add(column);
		return column;
	}

	public WishColumn<?> addChild(String title, int width)
	{
		WishColumn<?> column = new WishColumn<>(title, width);
		children.add(column);
		return column;
	}

	public WishColumn(String title, List<WishColumn<?>> children)
	{
		super();
		this.title = title;
		this.children = children;
	}

	public WishColumn(String title, int width)
	{
		super();
		this.title = title;
		this.width = width;
	}

	public WishColumn(String title)
	{
		super();
		this.title = title;
	}

	public WishColumn(String title, WishExcelFunction<T> customRender)
	{
		super();
		this.title = title;
		this.functions.add(customRender);
	}

	public WishColumn(String title, int width, WishExcelFunction<T> customRender)
	{
		super();
		this.title = title;
		this.width = width;
		this.functions.add(customRender);
	}

	public String getTitle()
	{
		return title;
	}

	public void setTitle(String title)
	{
		this.title = title;
	}

	public int getWidth()
	{
		return width;
	}

	public WishColumn<?> setWidth(int width)
	{
		this.width = width;
		return this;
	}

	public List<WishColumn<?>> getChildren()
	{
		return children;
	}

	public void setChildren(List<WishColumn<?>> children)
	{
		this.children = children;
	}

	public List<WishExcelFunction<?>> getFunctions()
	{
		return functions;
	}

	public void setFunctions(List<WishExcelFunction<?>> functions)
	{
		this.functions = functions;
	}

	private static List<String> getHeaders(WishColumn<?> tableHeader, List<List<String>> lists, List<String> list)
	{
		list.add(tableHeader.getTitle());
		if (tableHeader.getChildren() != null && tableHeader.getChildren().size() > 0)
		{
			tableHeader.getChildren().forEach(item -> {
				List<String> newlist = new ArrayList<>(list);
				getHeaders(item, lists, newlist);
			});
		}
		else
		{
			lists.add(list);
		}
		return list;
	}

	static <T> List<List<String>> getHeaders(List<WishColumn<?>> list)
	{
		List<List<String>> lists = new ArrayList<>();
		list.forEach(item -> {
			getHeaders(item, lists, new ArrayList<>());
		});
		return lists;
	}

	static <T> List<WishColumn<?>> getLeafs(List<WishColumn<?>> list)
	{
		List<WishColumn<?>> result = new ArrayList<>();
		list.forEach(item -> {
			if (item.getChildren().isEmpty())
			{
				result.add(item);
			}
			else
			{
				result.addAll(getLeafs(item.getChildren()));
			}
		});
		return result;
	}
}
